package com.wanpinghui.wphlibrary.model.user;

import android.util.SparseArray;

import com.wanpinghui.wphlibrary.common.IWPHApi;
import com.wanpinghui.wphlibrary.model.oauth.UserInfoUtil;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserBean;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserInformation;
import com.wanpinghui.wphlibrary.model.order.bean.PageDataBean;
import com.wanpinghui.wphlibrary.model.user.bean.AvaterBean;
import com.wanpinghui.wphlibrary.model.user.bean.BusinessTypeBean;
import com.wanpinghui.wphlibrary.model.user.bean.CompanyBean;
import com.wanpinghui.wphlibrary.model.user.bean.CreditRuleBean;
import com.wanpinghui.wphlibrary.model.user.bean.GetBusinessResult;
import com.wanpinghui.wphlibrary.model.user.bean.GetRechargeResult;
import com.wanpinghui.wphlibrary.model.user.bean.RechargePlansBean;
import com.yunshl.yunshllibrary.utils.TextUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit.Network;
import retrofit.callback.YRequestCallback;
import retrofit.callback.YunShlAction;
import retrofit.callback.YunShlResult;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 功能描述
 * author:caitiangui
 * time:2017/12/4
 */

public class UserInfoServiceImp implements UserInfoService {
    @Override
    public void avatar(String base64, final YRequestCallback<AvaterBean> callback) {
        Map<String,Object> params = new HashMap<>();
        params.put("img", base64);
        Network.getApi(IWPHApi.class).avatar(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<AvaterBean>>() {
                    @Override
                    public void call(YunShlResult<AvaterBean> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void credit_rule(final YRequestCallback<List<CreditRuleBean>> callback) {
        Network.getApi(IWPHApi.class).credit_rule()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<List<CreditRuleBean>>>() {
                    @Override
                    public void call(YunShlResult<List<CreditRuleBean>> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void buySmsPoint(int id, final YRequestCallback<Object> callback) {
        Map<String,Object> params = new HashMap<>();
        params.put("set_id", id);
        Network.getApi(IWPHApi.class)
                .buySmsPoint(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<Object>>() {
                    @Override
                    public void call(YunShlResult<Object> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void buyDispatchPoint(int id, final YRequestCallback<Object> callback) {
        Map<String,Object> params = new HashMap<>();
        params.put("set_id", id);
        Network.getApi(IWPHApi.class)
                .buyDispatchPoint(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<Object>>() {
                    @Override
                    public void call(YunShlResult<Object> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    public void getPlansList(final YRequestCallback<GetRechargeResult> callback) {
        Network.getApi(IWPHApi.class).getRechargePlansList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<GetRechargeResult>>() {
                    @Override
                    public void call(YunShlResult<GetRechargeResult> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void getBusinissData(final YRequestCallback<GetBusinessResult.TagBean> callback) {
        Network.getApi(IWPHApi.class).getMyBusiness()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<GetBusinessResult>>() {
                    @Override
                    public void call(YunShlResult<GetBusinessResult> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body.getTags());
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void areasScope(boolean area_type, ArrayList<String> area_id, final YRequestCallback<Object> callback) {
        Map<String,Object> params = new HashMap<>();
        params.put("area_type",area_type);
        params.put("area_id", area_id);
        System.out.println("area_id =>"+ area_type + area_id);
        if (area_type ) {
            Network.getApi(IWPHApi.class).areasScope(params)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<YunShlResult<Object>>() {
                        @Override
                        public void call(YunShlResult<Object> result) {
                            if (result.isSuccess()) {
                                UserBean userBean = UserInfoUtil.getUserInfo();
                                if (userBean != null) {
                                    userBean.getUser().setHas_set_business(true);
                                    UserInfoUtil.saveUserInfo(userBean);
                                }
                                if (callback != null) {
                                    callback.onSuccess(result.body);
                                }
                            } else {
                                if (callback != null) {
                                    callback.onFailed(result.status, result.msg);
                                }
                            }
                        }
                    }, new YunShlAction<Throwable>(callback));
        }else if (area_id!= null && area_id.size()>0){
            Network.getApi(IWPHApi.class).areasScope(params)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<YunShlResult<Object>>() {
                        @Override
                        public void call(YunShlResult<Object> result) {
                            if (result.isSuccess()) {
                                UserBean userBean = UserInfoUtil.getUserInfo();
                                if (userBean != null) {
                                    userBean.getUser().setHas_set_business(true);
                                    UserInfoUtil.saveUserInfo(userBean);
                                }
                                if (callback != null) {
                                    callback.onSuccess(result.body);
                                }
                            } else {
                                if (callback != null) {
                                    callback.onFailed(result.status, result.msg);
                                }
                            }
                        }
                    }, new YunShlAction<Throwable>(callback));
        }else {
            if (callback != null) {
                callback.onFailed(0, "请至少选择一个城市");
            }
        }
    }

    @Override
    public void businessScope(List<BusinessTypeBean> bean, final YRequestCallback<Object> callback) {
        Map<String,Object> params = new HashMap<>();
        params.put(bean.get(3).getKey(),bean.get(3).isSelect() );
        params.put(bean.get(2).getKey(), bean.get(2).isSelect());
        params.put(bean.get(1).getKey(), bean.get(1).isSelect());
        params.put(bean.get(0).getKey(), bean.get(0).isSelect());
        if (ishaveSelect(bean)){
            Network.getApi(IWPHApi.class).businessScope(params)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<YunShlResult<Object>>() {
                        @Override
                        public void call(YunShlResult<Object> result) {
                            if (result.isSuccess()) {
                                UserBean userBean = UserInfoUtil.getUserInfo();
                                if (userBean != null) {
                                    userBean.getUser().setHas_set_business(true);
                                    UserInfoUtil.saveUserInfo(userBean);
                                }
                                if (callback != null) {
                                    callback.onSuccess(result.body);
                                }
                            }else {
                                if (callback != null) {
                                    callback.onFailed(result.status, result.msg);
                                }
                            }
                        }
                    }, new YunShlAction<Throwable>(callback));
        }else {
            if (callback != null) {
                callback.onFailed(0, "至少选择从事一种业务");
            }
        }

    }

    private boolean ishaveSelect(List<BusinessTypeBean> bean){
        for (BusinessTypeBean bean1 : bean){
            if (bean1.isSelect())
                return true;
        }
        return false;
    }

    @Override
    public void UpdateUserInfo(final UserInformation bean, final YRequestCallback<UserInformation> callback) {
        Map<String,Object> params = new HashMap<>();
        params.put("nickname",bean.getNickname() );
        params.put("gender", bean.getGender());
//        params.put("addr", bean.getAddr());
        if (TextUtil.isNotEmpty(bean.getCom())){
            params.put("com", bean.getCom());
        }
//        params.put("pos",bean.getPos());
        Network.getApi(IWPHApi.class).UpdateUserInfo(params).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<UserInformation>>() {
                    @Override
                    public void call(YunShlResult<UserInformation> result) {
                        if (callback != null){
                            if (result.isSuccess()){
                                UserBean userBean = UserInfoUtil.getUserInfo();
                                if (result.body != null && userBean != null) {
                                        userBean.setUser(bean);
                                        UserInfoUtil.saveUserInfo(userBean);
                                }
                                callback.onSuccess(result.body);
                            }else {
                                callback.onFailed(result.status, result.msg);
                            }

                        }
                    }
                },new YunShlAction<Throwable>(callback));

    }

    @Override
    public void getMyCompany(final YRequestCallback<CompanyBean> callback) {
        Network.getYSApi(IWPHApi.class)
                .getMyCompany()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<CompanyBean>>() {
                    @Override
                    public void call(YunShlResult<CompanyBean> result) {
                                 if (callback != null){
                                     if (result.isSuccess()){
                                         if (result.body != null){
                                             callback.onSuccess(result.body);
                                         }else {
                                             callback.onFailed(0,"获取数据错误");
                                         }
                                     }else {
                                         callback.onFailed(0,result.msg);
                                     }
                                 }
                    }
                },new YunShlAction<Throwable>(callback));
        }

    @Override
    public void getMyDraftsCompany(final YRequestCallback<CompanyBean> callback) {
        Network.getYSApi(IWPHApi.class)
                .getMyDraftsCompany()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<CompanyBean>>() {
                    @Override
                    public void call(YunShlResult<CompanyBean> result) {
                        if (callback != null){
                            if (result.isSuccess()){
                                if (result.body != null){
                                    callback.onSuccess(result.body);
                                }else {
                                    callback.onFailed(0,"获取数据错误");
                                }
                            }else {
                                callback.onFailed(0,result.msg);
                            }
                        }
                    }
                },new YunShlAction<Throwable>(callback));
    }

    @Override
    public void searchCompanyList(String keyword_,int currentPage,final YRequestCallback<PageDataBean<CompanyBean>> callback) {
        Network.getYSApi(IWPHApi.class)
                .searchCompanyList(currentPage,keyword_)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<PageDataBean<CompanyBean>>>() {
                    @Override
                    public void call(YunShlResult<PageDataBean<CompanyBean>> result) {
                        if (callback != null){
                            if (result.isSuccess()){
                                if (result.body != null){
                                    callback.onSuccess(result.body);
                                }else {
                                    callback.onFailed(0,"获取数据错误");
                                }
                            }else {
                                callback.onFailed(0,result.msg);
                            }
                        }
                    }
                },new YunShlAction<Throwable>(callback));
    }

    @Override
    public void saveCompanyName(CompanyBean bean, final YRequestCallback<CompanyBean> callback) {
        Network.getYSApi(IWPHApi.class).saveCompanyName(bean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<CompanyBean>>() {
                    @Override
                    public void call(YunShlResult<CompanyBean> result) {
                        if (callback != null){
                            if (result.isSuccess()){

                                    callback.onSuccess(result.body);

                            }else {
                                callback.onFailed(0,result.msg);
                            }
                        }
                    }
                },new YunShlAction<Throwable>(callback));
    }

    @Override
    public void saveCompanyBase(CompanyBean bean, final YRequestCallback<CompanyBean> callback) {
        Network.getYSApi(IWPHApi.class).saveCompanyBase(bean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<CompanyBean>>() {
                    @Override
                    public void call(YunShlResult<CompanyBean> result) {
                        if (callback != null){
                            if (result.isSuccess()){

                                    callback.onSuccess(result.body);

                            }else {
                                callback.onFailed(0,result.msg);
                            }
                        }
                    }
                },new YunShlAction<Throwable>(callback));
    }

    @Override
    public void saveCompanyCaseAndCourse(CompanyBean bean, final YRequestCallback<CompanyBean> callback) {
        Network.getYSApi(IWPHApi.class).saveCompanyCaseAndCourse(bean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<CompanyBean>>() {
                    @Override
                    public void call(YunShlResult<CompanyBean> result) {
                        if (callback != null){
                            if (result.isSuccess()){

                                    callback.onSuccess(result.body);

                            }else {
                                callback.onFailed(0,result.msg);
                            }
                        }
                    }
                },new YunShlAction<Throwable>(callback));
    }

    @Override
    public void saveCompanyRegister(CompanyBean bean, final YRequestCallback<CompanyBean> callback) {
        Network.getYSApi(IWPHApi.class).saveCompanyRegister(bean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<CompanyBean>>() {
                    @Override
                    public void call(YunShlResult<CompanyBean> result) {
                        if (callback != null){
                            if (result.isSuccess()){

                                    callback.onSuccess(result.body);

                            }else {
                                callback.onFailed(0,result.msg);
                            }
                        }
                    }
                },new YunShlAction<Throwable>(callback));
    }

    @Override
    public void saveCompanySkill(CompanyBean bean, final YRequestCallback<CompanyBean> callback) {
        Network.getYSApi(IWPHApi.class).saveCompanySkill(bean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<CompanyBean>>() {
                    @Override
                    public void call(YunShlResult<CompanyBean> result) {
                        if (callback != null){
                            if (result.isSuccess()){

                                    callback.onSuccess(result.body);


                            }else {
                                callback.onFailed(0,result.msg);
                            }
                        }
                    }
                },new YunShlAction<Throwable>(callback));
    }

    @Override
    public void saveDrafts(CompanyBean bean, final YRequestCallback<CompanyBean> callback) {
        Network.getYSApi(IWPHApi.class).saveDrafts(bean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<CompanyBean>>() {
                    @Override
                    public void call(YunShlResult<CompanyBean> result) {
                        if (callback != null){
                            if (result.isSuccess()){

                                    callback.onSuccess(result.body);

                            }else {
                                callback.onFailed(0,result.msg);
                            }
                        }
                    }
                },new YunShlAction<Throwable>(callback));
    }

    @Override
    public void joinCompany(long id, final YRequestCallback<CompanyBean> callback) {
        Network.getYSApi(IWPHApi.class).joinCompany(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<CompanyBean>>() {
                    @Override
                    public void call(YunShlResult<CompanyBean> result) {
                        if (callback != null){
                            if (result.isSuccess()){

                                callback.onSuccess(result.body);


                            }else {
                                callback.onFailed(0,result.msg);
                            }
                        }
                    }
                },new YunShlAction<Throwable>(callback));
    }
}
